#include "../reader.h"

#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define  READER_DEV_PATH            "/dev/CST-RC500"
#define  CPU_CARD_FILE_NAME         "1PAY.SYS.DDF01"
static   s8 CPU_CARD_FILE_PATH[8]  = {0xDF, 0x10};
static   s8 PSAM_CARD_FILE_PATH[8] = {0xDF, 0x10};
 
int main(int argv, char **argc)
{
    int index = 1;
	char path_name[3][16];
	reader_op *reader = NULL;
	int ret = 0;

	if (argv == 3) {
		ret = strcmp("DEBUG", argc[1]);
		if (!ret) {
			DEBUG_APP_WALL = atoi(argc[2]);
		}
	}


	reader = reader_attr_init(READER_DEV_PATH);
	if (!reader) {
		printf("DEBUG_info: reader init failed\n");
		
		return 0;
	}

	strncpy(&path_name[0][0], CPU_CARD_FILE_NAME, strlen(CPU_CARD_FILE_NAME));
	reader->deal_in_data.name = &path_name[0][0];
	reader->deal_in_data.path = PSAM_CARD_FILE_PATH;
	reader->psam_in_data.psam_path = PSAM_CARD_FILE_PATH;

	ret = reader_search_start(reader);
	if (ret) {
		printf("reader seach abort!!!\n");
		return 0;
	}

    reader->psam_out_data.deal_index[0] = index;
    reader->psam_out_data.deal_index[1] = index << 8;
    reader->psam_out_data.deal_index[2] = index << 16;
    reader->psam_out_data.deal_index[3] = index << 24;

    reader->deal_in_data.money_val[0] = 0x56;
    reader->deal_in_data.money_val[1] = 0x34;
    reader->deal_in_data.money_val[2] = 0x12;
    reader->deal_in_data.money_val[3] = 0x00;

	reader->deal_in_data.deal_val[0] = 100; // 1
	reader->deal_in_data.deal_val[1] = 0;
	reader->deal_in_data.deal_val[2] = 0;
	reader->deal_in_data.deal_val[3] = 0;
	reader->deal_in_data.deal_rebate[0] = 0;
	reader->deal_in_data.deal_rebate[1] = 0;
	reader->deal_in_data.deal_rebate[2] = 0;
	reader->deal_in_data.deal_rebate[3] = 0;
	reader->deal_in_data.deal_type = 0x01;
	reader->deal_in_data.deal_time[0] = 0x20;
	reader->deal_in_data.deal_time[1] = 0x15;
	reader->deal_in_data.deal_time[2] = 0x06;
	reader->deal_in_data.deal_time[3] = 0x05;
	reader->deal_in_data.deal_time[4] = 0x18;
	reader->deal_in_data.deal_time[5] = 0x47;
	reader->deal_in_data.deal_time[6] = 0x20;
	reader->deal_in_data.card_id[0] = 0x86;
	reader->deal_in_data.card_id[1] = 0x00;
	reader->deal_in_data.card_id[2] = 0x02;
	reader->deal_in_data.card_id[3] = 0x00;
	reader->deal_in_data.card_id[4] = 0x00;
	reader->deal_in_data.card_id[5] = 0x00;
	reader->deal_in_data.card_id[6] = 0x00;
	reader->deal_in_data.card_id[7] = 0x01;

	while(1) {
		if (reader->status == FIND_CARD_SUCC) {
			switch (reader->type) {
				case READER_CONTACTLESS_CPU:
					if (reader->deal_op) {
						// reader->deal_op((void *)reader);
					}

					if (reader->load_op) {
						reader->load_op((void *)reader);
					}
					sleep(1);
					reader->status = FIND_CARD_FAIL;
					break;

				case READER_CONTACTLESS_M1:
					if (reader->deal_op) {
                    	reader->deal_op((void *)reader);
					}

					sleep(1);
					reader->status = FIND_CARD_FAIL;
					break;

				default:
					break;
			}
		}
		usleep(500*1000);
	}
	
	return 0;
}

